package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Brand;
import com.cskaoyan.bean.pojo.BrandExample;
import com.cskaoyan.bean.wx.brand.BrandDataVo;
import com.cskaoyan.bean.wx.brand.BrandDetailVo;
import com.cskaoyan.bean.wx.brand.BrandNewVo;
import com.cskaoyan.bean.wx.brand.BrandVo;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.service.wx.WxBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Name : WxBrandServiceImpl.java
 * @Time : 2021/8/14 23:24
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
public class WxBrandServiceImpl implements WxBrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandDetailVo getBrandDetail(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        BrandDetailVo brandDetailVo = new BrandDetailVo();
        BrandNewVo brandVo = new BrandNewVo();
        if (!brand.getDeleted()){
            brandVo.setId(brand.getId());
            brandVo.setName(brand.getName());
            brandVo.setDesc(brand.getDesc());
            brandVo.setPicUrl(brand.getPicUrl());
            brandVo.setSortOrder(brand.getSortOrder());
            brandVo.setFloorPrice(brand.getFloorPrice());
            brandVo.setAddTime(brand.getAddTime());
            brandVo.setUpdateTime(brand.getUpdateTime());
            brandVo.setDeleted(brand.getDeleted());
        }
        brandDetailVo.setBrand(brandVo);
        return brandDetailVo;
    }

    @Override
    public BrandDataVo getBrandList(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<BrandVo> brandVos = new ArrayList<>();
        for (Brand brand : brands) {
            BrandVo brandVo = new BrandVo();
            brandVo.setId(brand.getId());
            brandVo.setName(brand.getName());
            brandVo.setDesc(brand.getDesc());
            brandVo.setPicUrl(brand.getPicUrl());
            brandVo.setFloorPrice(brand.getFloorPrice());
            brandVos.add(brandVo);
        }
        PageInfo<Brand> pageInfo = new PageInfo<>();
        int total = pageInfo.getSize();
        return BrandDataVo.create(brandVos,total);
    }
}
